package com.cresch.ical.downloader.ical;
import java.util.ArrayList;

/**
 * 
 * @author Resch Christian
 *
 */
public class Splitter implements SplitterInterface {
	private String startTag;
	private String endTag;
	
	public Splitter() 
	{
		startTag = new String("");
		endTag = new String("");
	}
	
	public void Initialize(String startTag, String endTag)
	{
		this.startTag = startTag;
		this.endTag = endTag;
	}

	public ArrayList<String> splittUp(String input)
	{
		ArrayList<String> splittedList = new ArrayList<String>();
		int beginIndex = -1;
		int endIndex = -1;
		
		// find start index of element
		while ((beginIndex = input.indexOf(this.startTag, beginIndex)) != -1) {
			
			// find end index of elemtent
			endIndex = input.indexOf(this.endTag, beginIndex);
			if (endIndex == -1) {
				// no endtag found
				break;
			}
			
			// add found element to result array
			String splittedElement;
			splittedElement = input.substring(beginIndex+this.startTag.length(), endIndex);
			splittedList.add(splittedElement);
			
			// set begin index to the end of the found element
			beginIndex = endIndex;
		}
		
		return splittedList;
	}
}
